package com.system.base;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
public class MyRejectedHandler implements RejectedExecutionHandler {
	
	@Autowired
	private RedisUtil redisUtil;

	public MyRejectedHandler(RedisUtil redisUtil) {
		this.redisUtil = redisUtil;
	}

	public MyRejectedHandler() {

	}

	@Override
	public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
		try {
			MyTask task = (MyTask) r;
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("orderId", task.getOrderId());
			jsonObject.put("taskeName", task.getTaskName());
			jsonObject.put("resourceType", task.getResourceType());
			// redisUtil.lSet("threadPoolTask", jsonObject.toJSONString());
			String jsonStr = JSONUtil.toJsonStr(task);
			redisUtil.lSet("threadPoolTask", jsonStr);
			log.info("当前被拒绝任务为：" + jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}